
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>3.5 Json与Json数据获取 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="实例：糗事百科网站抓取.html" />
    
    
    <link rel="prev" href="Xpath爬虫案例.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="正则表达式.html">
            
                <a href="正则表达式.html">
            
                    
                    第一章：正则表达式
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.2.1" data-path="正则表达式入门.html">
            
                <a href="正则表达式入门.html">
            
                    
                    1.1 正则表达式入门
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.2" data-path="Python中的正则表达式.html">
            
                <a href="Python中的正则表达式.html">
            
                    
                    1.2 Python中的正则表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.3" data-path="基本正则表达式.html">
            
                <a href="基本正则表达式.html">
            
                    
                    1.3 基本正则表达式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.4" data-path="分组.html">
            
                <a href="分组.html">
            
                    
                    1.4 分组
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.5" data-path="先行断言.html">
            
                <a href="先行断言.html">
            
                    
                    1.5 先行断言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.6" data-path="标记.html">
            
                <a href="标记.html">
            
                    
                    1.6 标记
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.7" data-path="替换与正则表达式编译.html">
            
                <a href="替换与正则表达式编译.html">
            
                    
                    1.7 替换与正则编译
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2.8" data-path="正则表达式练习.html">
            
                <a href="正则表达式练习.html">
            
                    
                    1.8 正则表达式练习
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="Python爬虫学习目标.html">
            
                <a href="Python爬虫学习目标.html">
            
                    
                    第二章：爬虫与Requests
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="Requests的简单使用.html">
            
                <a href="Requests的简单使用.html">
            
                    
                    2.1 Requests的简单使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="深入了解Requests.html">
            
                <a href="深入了解Requests.html">
            
                    
                    2.2 深入了解Requets
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="结构化与非结构化.html">
            
                <a href="结构化与非结构化.html">
            
                    
                    第三章：非结构化与结构化数据提取
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="正则抓取.html">
            
                <a href="正则抓取.html">
            
                    
                    3.1 使用正则的爬虫
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="xpath与XML.html">
            
                <a href="xpath与XML.html">
            
                    
                    3.2 Xpath与XML类库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="lxml.html">
            
                <a href="lxml.html">
            
                    
                    3.3 LXML库的使用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="Xpath爬虫案例.html">
            
                <a href="Xpath爬虫案例.html">
            
                    
                    3.4 案例：贴吧图片
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.5" data-path="Json与JsonPath.html">
            
                <a href="Json与JsonPath.html">
            
                    
                    3.5 Json与Json数据获取
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="实例：糗事百科网站抓取.html">
            
                <a href="实例：糗事百科网站抓取.html">
            
                    
                    3.6 糗事百科网站爬虫
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >3.5 Json与Json数据获取</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="json&#x6570;&#x636E;&#x7ED3;&#x6784;">JSON&#x6570;&#x636E;&#x7ED3;&#x6784;</h1>
<p>JSON(JavaScript Object Notation) &#x662F;&#x4E00;&#x79CD;&#x8F7B;&#x91CF;&#x7EA7;&#x7684;&#x6570;&#x636E;&#x4EA4;&#x6362;&#x683C;&#x5F0F;&#xFF0C;&#x5B83;&#x4F7F;&#x5F97;&#x4EBA;&#x4EEC;&#x5F88;&#x5BB9;&#x6613;&#x7684;&#x8FDB;&#x884C;&#x9605;&#x8BFB;&#x548C;&#x7F16;&#x5199;&#x3002;&#x540C;&#x65F6;&#x4E5F;&#x65B9;&#x4FBF;&#x4E86;&#x673A;&#x5668;&#x8FDB;&#x884C;&#x89E3;&#x6790;&#x548C;&#x751F;&#x6210;&#x3002;&#x9002;&#x7528;&#x4E8E;&#x8FDB;&#x884C;&#x6570;&#x636E;&#x4EA4;&#x4E92;&#x7684;&#x573A;&#x666F;&#xFF0C;&#x6BD4;&#x5982;&#x7F51;&#x7AD9;&#x524D;&#x53F0;&#x4E0E;&#x540E;&#x53F0;&#x4E4B;&#x95F4;&#x7684;&#x6570;&#x636E;&#x4EA4;&#x4E92;&#x3002;</p>
<p>JSON&#x548C;XML&#x7684;&#x6BD4;&#x8F83;&#x53EF;&#x8C13;&#x4E0D;&#x76F8;&#x4E0A;&#x4E0B;&#x3002;</p>
<p>Python 3&#x4E2D;&#x81EA;&#x5E26;&#x4E86;JSON&#x6A21;&#x5757;&#xFF0C;&#x76F4;&#x63A5;import json&#x5C31;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E86;&#x3002;</p>
<p>&#x5B98;&#x65B9;&#x6587;&#x6863;&#xFF1A;<a href="http://docs.python.org/library/json.html" target="_blank">http://docs.python.org/library/json.html</a></p>
<p>Json&#x5728;&#x7EBF;&#x89E3;&#x6790;&#x7F51;&#x7AD9;&#xFF1A;<a href="http://www.json.cn/#" target="_blank">http://www.json.cn/#</a></p>
<p>&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;Json&#x6570;&#x636E;&#xFF1A;</p>
<pre><code class="lang-json">{
    <span class="hljs-string">&quot;name&quot;</span>:<span class="hljs-string">&quot;&#x5F20;&#x4E09;&quot;</span>,
    <span class="hljs-string">&quot;age&quot;</span>:<span class="hljs-number">19</span>,
    <span class="hljs-string">&quot;major&quot;</span>:[<span class="hljs-string">&quot;&#x6316;&#x6398;&#x673A;&quot;</span>,<span class="hljs-string">&quot;&#x7092;&#x83DC;&quot;</span>,<span class="hljs-string">&quot;&#x7F16;&#x7A0B;&quot;</span>],
    <span class="hljs-string">&quot;hasCar&quot;</span>:<span class="hljs-literal">false</span>,
    <span class="hljs-string">&quot;child&quot;</span>:[{<span class="hljs-string">&quot;name&quot;</span>:<span class="hljs-string">&quot;&#x5C0F;&#x660E;&quot;</span>,<span class="hljs-string">&quot;age&quot;</span>:<span class="hljs-number">2</span>},{<span class="hljs-string">&quot;name&quot;</span>:<span class="hljs-string">&quot;&#x5C0F;&#x82B1;&quot;</span>,<span class="hljs-string">&quot;age&quot;</span>:<span class="hljs-number">2</span>}]
}
</code></pre>
<h2 id="python&#x4E2D;&#x7684;json&#x6A21;&#x5757;">Python&#x4E2D;&#x7684;Json&#x6A21;&#x5757;</h2>
<p>&#x5BFC;&#x5165;json&#x5373;&#x53EF;&#x5F00;&#x59CB;&#x4F7F;&#x7528;</p>
<pre><code>import json
</code></pre><p>json&#x6A21;&#x5757;&#x63D0;&#x4F9B;&#x4E86;&#x56DB;&#x4E2A;&#x529F;&#x80FD;&#xFF1A;dumps&#x3001;dump&#x3001;loads&#x3001;load&#xFF0C;&#x7528;&#x4E8E;&#x5B57;&#x7B26;&#x4E32; &#x548C; python&#x6570;&#x636E;&#x7C7B;&#x578B;&#x95F4;&#x8FDB;&#x884C;&#x8F6C;&#x6362;&#x3002;</p>
<h3 id="1-jsonloads">1. json.loads()</h3>
<table>
<thead>
<tr>
<th>JSON</th>
<th>Python</th>
</tr>
</thead>
<tbody>
<tr>
<td>object</td>
<td>dict</td>
</tr>
<tr>
<td>array</td>
<td>list</td>
</tr>
<tr>
<td>string</td>
<td>unicode</td>
</tr>
<tr>
<td>number</td>
<td>(int)    int, long</td>
</tr>
<tr>
<td>number</td>
<td>(real)    float</td>
</tr>
<tr>
<td>true</td>
<td>True</td>
</tr>
<tr>
<td>false</td>
<td>False</td>
</tr>
<tr>
<td>null</td>
<td>None</td>
</tr>
</tbody>
</table>
<pre><code>import json

strList = &apos;[1, 2, 3, 4]&apos;

strDict = &apos;{&quot;city&quot;: &quot;&#x5317;&#x4EAC;&quot;, &quot;name&quot;: &quot;&#x5927;&#x732B;&quot;}&apos;

json.loads(strList)
# [1, 2, 3, 4]

json.loads(strDict) # json&#x6570;&#x636E;&#x81EA;&#x52A8;&#x6309;Unicode&#x5B58;&#x50A8;
# {u&apos;city&apos;: u&apos;\u5317\u4eac&apos;, u&apos;name&apos;: u&apos;\u5927\u732b&apos;}
</code></pre><h3 id="2-jsondumps">2. json.dumps()</h3>
<p>&#x5B9E;&#x73B0;python&#x7C7B;&#x578B;&#x8F6C;&#x5316;&#x4E3A;json&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;str&#x5BF9;&#x8C61; &#x628A;&#x4E00;&#x4E2A;Python&#x5BF9;&#x8C61;&#x7F16;&#x7801;&#x8F6C;&#x6362;&#x6210;Json&#x5B57;&#x7B26;&#x4E32;</p>
<p>&#x4ECE;python&#x539F;&#x59CB;&#x7C7B;&#x578B;&#x5411;json&#x7C7B;&#x578B;&#x7684;&#x8F6C;&#x5316;&#x5BF9;&#x7167;&#x5982;&#x4E0B;&#xFF1A;</p>
<table>
<thead>
<tr>
<th>Python</th>
<th>JSON</th>
</tr>
</thead>
<tbody>
<tr>
<td>dict</td>
<td>object</td>
</tr>
<tr>
<td>list, tuple</td>
<td>array</td>
</tr>
<tr>
<td>str, unicode</td>
<td>string</td>
</tr>
<tr>
<td>int, long, float</td>
<td>number</td>
</tr>
<tr>
<td>True</td>
<td>true</td>
</tr>
<tr>
<td>False</td>
<td>false</td>
</tr>
<tr>
<td>None</td>
<td>null</td>
</tr>
</tbody>
</table>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> json
<span class="hljs-keyword">import</span> chardet

listStr = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]
tupleStr = (<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>)
dictStr = {<span class="hljs-string">&quot;city&quot;</span>: <span class="hljs-string">&quot;&#x5317;&#x4EAC;&quot;</span>, <span class="hljs-string">&quot;name&quot;</span>: <span class="hljs-string">&quot;&#x5927;&#x732B;&quot;</span>}

json.dumps(listStr)
<span class="hljs-comment"># &apos;[1, 2, 3, 4]&apos;</span>
json.dumps(tupleStr)
<span class="hljs-comment"># &apos;[1, 2, 3, 4]&apos;</span>

<span class="hljs-comment"># &#x6CE8;&#x610F;&#xFF1A;json.dumps() &#x5E8F;&#x5217;&#x5316;&#x65F6;&#x9ED8;&#x8BA4;&#x4F7F;&#x7528;&#x7684;ascii&#x7F16;&#x7801;</span>
<span class="hljs-comment"># &#x6DFB;&#x52A0;&#x53C2;&#x6570; ensure_ascii=False &#x7981;&#x7528;ascii&#x7F16;&#x7801;&#xFF0C;&#x6309;utf-8&#x7F16;&#x7801;</span>
<span class="hljs-comment"># chardet.detect()&#x8FD4;&#x56DE;&#x5B57;&#x5178;, &#x5176;&#x4E2D;confidence&#x662F;&#x68C0;&#x6D4B;&#x7CBE;&#x786E;&#x5EA6;</span>

json.dumps(dictStr)
<span class="hljs-comment"># &apos;{&quot;city&quot;: &quot;\\u5317\\u4eac&quot;, &quot;name&quot;: &quot;\\u5927\\u5218&quot;}&apos;</span>

chardet.detect(str(json.dumps(dictStr)).encode())
<span class="hljs-comment"># {&apos;confidence&apos;: 1.0, &apos;encoding&apos;: &apos;ascii&apos;}</span>

print(json.dumps(dictStr, ensure_ascii=<span class="hljs-keyword">False</span>))
<span class="hljs-comment"># {&quot;city&quot;: &quot;&#x5317;&#x4EAC;&quot;, &quot;name&quot;: &quot;&#x5927;&#x5218;&quot;}</span>

chardet.detect(json.dumps(dictStr, ensure_ascii=<span class="hljs-keyword">False</span>))
<span class="hljs-comment"># {&apos;confidence&apos;: 0.99, &apos;encoding&apos;: &apos;utf-8&apos;}</span>
</code></pre>
<h3 id="3jsondump">3.json.dump()</h3>
<p>&#x5C06;Python&#x5185;&#x7F6E;&#x7C7B;&#x578B;&#x5E8F;&#x5217;&#x5316;&#x4E3A;json&#x5BF9;&#x8C61;&#x540E;&#x5199;&#x5165;&#x6587;&#x4EF6;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> json

listStr = [{<span class="hljs-string">&quot;city&quot;</span>: <span class="hljs-string">&quot;&#x5317;&#x4EAC;&quot;</span>}, {<span class="hljs-string">&quot;name&quot;</span>: <span class="hljs-string">&quot;&#x5927;&#x5218;&quot;</span>}]
json.dump(listStr, open(<span class="hljs-string">&quot;listStr.json&quot;</span>,<span class="hljs-string">&quot;w&quot;</span>), ensure_ascii=<span class="hljs-keyword">False</span>)

dictStr = {<span class="hljs-string">&quot;city&quot;</span>: <span class="hljs-string">&quot;&#x5317;&#x4EAC;&quot;</span>, <span class="hljs-string">&quot;name&quot;</span>: <span class="hljs-string">&quot;&#x5927;&#x5218;&quot;</span>}
json.dump(dictStr, open(<span class="hljs-string">&quot;dictStr.json&quot;</span>,<span class="hljs-string">&quot;w&quot;</span>), ensure_ascii=<span class="hljs-keyword">False</span>)
</code></pre>
<h3 id="4jsonload">4.json.load()</h3>
<p>&#x8BFB;&#x53D6;&#x6587;&#x4EF6;&#x4E2D;json&#x5F62;&#x5F0F;&#x7684;&#x5B57;&#x7B26;&#x4E32;&#x5143;&#x7D20; &#x8F6C;&#x5316;&#x6210;python&#x7C7B;&#x578B;</p>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> json

strList = json.load(open(<span class="hljs-string">&quot;listStr.json&quot;</span>))
print(strList)

<span class="hljs-comment"># [{u&apos;city&apos;: u&apos;\u5317\u4eac&apos;}, {u&apos;name&apos;: u&apos;\u5927\u5218&apos;}]</span>

strDict = json.load(open(<span class="hljs-string">&quot;dictStr.json&quot;</span>))
print(strDict)
<span class="hljs-comment"># {u&apos;city&apos;: u&apos;\u5317\u4eac&apos;, u&apos;name&apos;: u&apos;\u5927\u5218&apos;}</span>
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="Xpath爬虫案例.html" class="navigation navigation-prev " aria-label="Previous page: 3.4 案例：贴吧图片">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="实例：糗事百科网站抓取.html" class="navigation navigation-next " aria-label="Next page: 3.6 糗事百科网站爬虫">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"3.5 Json与Json数据获取","level":"1.4.5","depth":2,"next":{"title":"3.6 糗事百科网站爬虫","level":"1.4.6","depth":2,"path":"notes/实例：糗事百科网站抓取.md","ref":"notes/实例：糗事百科网站抓取.md","articles":[]},"previous":{"title":"3.4 案例：贴吧图片","level":"1.4.4","depth":2,"path":"notes/Xpath爬虫案例.md","ref":"notes/Xpath爬虫案例.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"notes/Json与JsonPath.md","mtime":"2019-05-04T08:39:48.019Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2019-05-08T02:50:45.307Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

